package com.xiaoyao.novel.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.crawl.BaseCrawl;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.FileUtil;

public class K16Parser {
	private static Logger logger = Logger.getLogger(K16Parser.class);

	public static List<Chapter> parseBook(String page, Book book) {
		logger.debug("开始解析...");
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		// page = page.replaceFirst(".+?正文", "");
		List<Chapter> list = new ArrayList<Chapter>();
		book.list = list;
		// <li><a title="更新时间:2013-9-27 6:55:19	更新字数:3453" target="_blank"
		// href="4770209.shtml">第一章 后悔井</a></li>
		String bookRegex = "<li>\\s*<a.*?href='((\\d+).+?)'.*?>(.+?)</a>\\s*</li>";
		Pattern bookPattern = Pattern.compile(bookRegex);
		// System.out.println(page);
		Matcher matcher = bookPattern.matcher(page);
		while (matcher.find()) {
			String title = matcher.group(3).trim();
			if (title.startsWith("引子") || title.startsWith("前言") || title.startsWith("第") || title.matches("\\d+.+")) {
				logger.debug(title);
				Chapter chapter = new Chapter();
				list.add(chapter);
				chapter.bookId = book.bookId;
				chapter.chapterUrl = book.webId + "/" + matcher.group(1);
				chapter.webId = book.webId + "/" + matcher.group(2);
				chapter.chapterTitle = title;
				logger.debug(chapter);
			}
		}
		return list;
	}

	public static void parseChapter(String page, Chapter chapter) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		logger.debug("页面:" + page);
		String chapterRegex = "<!--开始-->(.+?)<!--E1ND-->";
		Pattern chapterPattern = Pattern.compile(chapterRegex);
		Matcher matcher = chapterPattern.matcher(page);
		if (matcher.find()) {
			chapter.chapterContent = matcher.group(1);
			logger.debug(chapter.chapterContent);
		} else {
			String chapterRegex2 = "<div.+?id='content'.+?</table>(.+?)</div>";
			Pattern chapterPattern2 = Pattern.compile(chapterRegex2);
			Matcher matcher2 = chapterPattern2.matcher(page);
			if (matcher2.find()) {
				chapter.chapterContent = matcher2.group(1);
				logger.debug(chapter.chapterContent);
			}

		}
	}

	/**
	 * 作者,[书名],简介,[类型],图片
	 * 
	 * @param page
	 * @param book
	 * @return
	 */
	public static Book parseDescrib(String page, Book book) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		logger.debug("页面:" + page);
		String infoRegex = "<ul class='booktitle tc b'>\\s*<li>.(.+?).</li>\\s*</ul>.+<img class='img'.+?src='(.+?)'";
		Pattern infoPattern = Pattern.compile(infoRegex);
		Matcher matcher = infoPattern.matcher(page);
		if (matcher.find()) {
			// book.bookName = matcher.group(1);
			if (ClassUtil.isEmpty(book.picPath)) {
				String picUrl = matcher.group(2);
				String fileName = book.bookId + ".jpg";
				fileName = BaseCrawl.downLoadImage(picUrl, fileName); // 抓图片
				book.picPath = FileUtil.PIC_DIR + fileName;
			}
		}

		String describRegex = "<li class='li11'>作品大类</li>\\s*<li class='li12'>.+?>(.*?)</a></li>.+?<li class='li11'>小说作者</li>\\s*<li class='li12'>(.+?)</li>.+?<ul class='booksum pd10 pd8 pd6 tl'><li>(.+?)<div";
		Pattern describPattern = Pattern.compile(describRegex);
		matcher = describPattern.matcher(page);
		if (matcher.find()) {
			// book.category = matcher.group(1);
			book.authorName = matcher.group(2);
			if (ClassUtil.isEmpty(book.describ)) {
				book.describ = matcher.group(3);
			}
		} else {
			logger.error("简介有问题!" + book.crawler);
		}

		/**
		 * <li class="li11">小说作者</li><li class="li12">唐家三少</li> <div
		 * class="block3">
		 * <ul class="BookUpdate pd10 tl">
		 * <li>&nbsp;&nbsp;<font color="#ff0000">[最新章节更新]&nbsp;&nbsp;<a
		 * href="/Html/Book/13/13438/4774104.shtml"><font
		 * color="#ff0000">第二百五十九章 幽幽&nbsp;&nbsp;更新时间:2013-10-2
		 * 8:07:23</font></a></font></li>
		 * </ul>
		 * <ul class="booksum pd10 pd8 pd6 tl">
		 * <li>
		 * <!--简介开始-->&nbsp;这里没有魔法，没有斗气，没有武术，却有武魂。唐门创立万年之后的斗罗大陆上，唐门式微。一代天骄横空出世
		 * ，新一代史莱克七怪能否重振唐门，谱写一曲绝世唐门之歌？<br>
		 * &nbsp;百万年魂兽，手握日月摘星辰的死灵圣法神，导致唐门衰落的全新魂导器体系。一切的神奇都将一一展现。<br>
		 * &nbsp;唐门暗器能否重振雄风，唐门能否重现辉煌，一切尽在绝世唐门！<br>
		 * &nbsp;【斗罗大陆II，我想，有这几个字就足够了吧。归来吧，唐门的兄弟姐妹】<br>
		 * &nbsp;v群群号：100489824（加群需要全订阅或粉丝值三千）YY：4095<br>
		 * <div class=
		 * "bdlikebutton bdlikebutton-blue bdlikebutton-large bdlikebutton-large-blue"
		 * ><div class="bdlikebutton-inner"><span
		 * class="bdlikebutton-add">+1</span><div
		 * class="bdlikebutton-count">301</div><div
		 * class="bdlikebutton-text">该内容对我有帮助</div></div></div> <!--简介结束--><br>
		 * <br>
		 * </li>
		 * </ul>
		 * </div>
		 */
		return book;
	}
}
